package com.naiterui.ehp.bs.patient.dao.mongo;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.naiterui.ehp.bs.patient.prodocl.scale.ScaleInfo;

@Repository
public class ApScaleMongoDao extends MongoBaseDao {

    private static final String COLLECTION_NAME = "m_scale_info";

    @Autowired
    private MongoTemplate mongoTemplate;

    public void save(ScaleInfo scaleInfo) {
        super.save(scaleInfo, COLLECTION_NAME);
    }

    @Override
    protected MongoTemplate getMongoTemplate() {
        return this.mongoTemplate;
    }

    /**
     * 根据表单ID，表单结果ID查询量表
     * ScaleMongoDao.getScaleInfo()
     *
     * @param formId
     * @param serialNumber
     *
     * @return
     *
     * @author wangchuan
     * @Date 2018年6月27日
     * @since V2.19.0
     */
    public ScaleInfo getScaleInfo(String formId, String serialNumber) {
        Criteria criteria = Criteria.where("formId").is(formId).and("serialNumber").is(serialNumber);
        Query query = Query.query(criteria);
        List<ScaleInfo> list = super.find(query, ScaleInfo.class, COLLECTION_NAME);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据业务id，是否覆盖状态查询量表信息
     *
     * @param extId
     * @param covered
     *
     * @return
     */
    public ScaleInfo getExtId(String extId, Integer covered, Integer isCompleted) {
        Criteria criteria = Criteria.where("extId").is(extId).and("covered").is(covered).and("isCompleted").is(isCompleted);
        Query query = Query.query(criteria);
        List<ScaleInfo> list = super.find(query, ScaleInfo.class, COLLECTION_NAME);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


    public ScaleInfo getScaleInfoByScaleId(String scaleId) {
        Criteria criteria = Criteria.where("id").is(scaleId);
        Query query = Query.query(criteria);
        List<ScaleInfo> list = super.find(query, ScaleInfo.class, COLLECTION_NAME);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public ScaleInfo getScaleInfoBySceneId(String sceneId) {
        Criteria criteria = Criteria.where("sceneId").is(sceneId);
        Query query = Query.query(criteria);
        List<ScaleInfo> list = super.find(query, ScaleInfo.class, COLLECTION_NAME);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public ScaleInfo getScaleInfoByComplete(String sceneId, Integer isCompleted) {
        Criteria criteria = Criteria.where("sceneId").is(sceneId).and("isCompleted").is(isCompleted);
        Query query = Query.query(criteria);
        List<ScaleInfo> list = super.find(query, ScaleInfo.class, COLLECTION_NAME);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public ScaleInfo getScaleByOrder(String sceneId, Integer isCompleted, Integer covered) {
        Criteria criteria = Criteria.where("sceneId").is(sceneId).and("covered").is(covered).and("isCompleted").is(isCompleted);
        Query query = Query.query(criteria);
        query.with(Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime")));
        List<ScaleInfo> list = super.find(query, ScaleInfo.class, COLLECTION_NAME);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public void update(String sceneId, Integer complete, Update update) {
        Query query = new Query();
        query.addCriteria(new Criteria("sceneId").is(sceneId));
        query.addCriteria(new Criteria("isCompleted").is(complete));
        super.update(ScaleInfo.class, COLLECTION_NAME, query, update);
    }

    public void updateById(String id, Update update) {
        Query query = new Query();
        query.addCriteria(new Criteria("id").is(id));
        super.update(ScaleInfo.class, COLLECTION_NAME, query, update);
    }

    public List<ScaleInfo> getScaleListByExtids(String[] extIdArr) {
        Criteria criteria = Criteria.where("extId")
                                    .in(extIdArr)
                                    .and("covered")
                                    .is(ScaleInfo.IS_COVERED_NO)
                                    .and("isCompleted")
                                    .is(ScaleInfo.IS_COMPLETED_YES);
        Query query = Query.query(criteria);
        List<ScaleInfo> resultList = super.find(query, ScaleInfo.class, COLLECTION_NAME);
        return resultList;
    }

    public List<ScaleInfo> getScaleListByDoctorIdAndFormIdAndCreateTime(Long doctorId, String fromId, Date startTime, Date endTime) {
        Criteria criteria = Criteria.where("doctorId").is(doctorId)
                                    .and("formId").is(fromId)
                                    .and("createTime").gte(startTime).lte(endTime);
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Query query = Query.query(criteria);
        query.with(sort);
        List<ScaleInfo> resultList = super.find(query, ScaleInfo.class, COLLECTION_NAME);
        return resultList;
    }

}
